package com.webcat.entity;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.webcat.enums.ResultEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @author wangjun
 * @date 18-2-9 下午6:09
 * @description
 * @modified by
 */

@Data
@Slf4j
public class Response<T> {

    private String code;

    private String msg;

    private T data;

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date serverTime;

    public static<T> Response<T> success(T data) {
        Response<T> response = new Response<>();
        response.setServerTime(new Date());
        response.setCode(ResultEnum.SUCCESS.getCode());
        response.setMsg(ResultEnum.SUCCESS.getMsg());
        response.setData(data);
        log.info(JSONObject.toJSONString(response));
        return response;
    }

    public static<T> Response<T> success() {
        Response<T> response = new Response<>();
        response.setServerTime(new Date());
        response.setCode(ResultEnum.SUCCESS.getCode());
        response.setMsg(ResultEnum.SUCCESS.getMsg());
        log.info(JSONObject.toJSONString(response));
        return response;
    }

    public static<T> Response<T> fail(String code, String errMsg) {
        Response<T> response = new Response<>();
        response.setServerTime(new Date());
        response.setCode(code);
        response.setMsg(errMsg);
        log.info(JSONObject.toJSONString(response));
        return response;
    }
}
